var player =null;
var playerTime=null;
var musicTime=0;
var playPosition=0;
var storage = window.localStorage;
$(function(){
	
	$("#navbar li a").click(function(){
		$("#player,#remote,#local").hide();
		if($(this).attr("name")=="palyer")
		{
			$("#player").show();
		}else if($(this).attr("name")=="remote")
		{
			$("#remote").show();
		}else if($(this).attr("name")=="local")
		{
			localMusicList();
			$("#local").show();
		}
	});
	
	var option={showText:false,
				barImage: 'images/progressbg_green.gif'
	           };
         showProgress(0,0);
})
 $(document).bind( "pageshow", function(event) {
	var playerHeight=$(document).height()-$("#footer").height()-$("#control").height()-$("#progress").height()-20;
	$("#lrc").css({height:playerHeight,width:$("#footer").width()-25});

});
document.addEventListener("deviceready", initMusicList, false);


 function initMusicList() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    }

    function gotFS(fileSystem) {
        fileSystem.root.getDirectory("TianTianMusic", {create: true, exclusive: false}, gotFileEntry, fail);
    }

    function gotFileEntry(fileEntry) {
      var read=  fileEntry.createReader();
		read.readEntries(gotFileWriter,null);
    }

    function gotFileWriter(entries) {
		var musicList=[];
				for(var i=0;i< entries.length;i++) {
					var name=entries[i].name.split(".");
					if(name[1]=="mp3")
					{
						musicList.push({name:name[0],singer:"",musicUrl:entries[i].toURI(),"pic":entries[i].pic});
					}
				}
				
				storage.setItem("musicList",JSON.stringify(musicList));
    }

    function fail(error) {
        console.log(error.code);
    }
//显示本地音乐列表
function localMusicList()
{
	var musicList=JSON.parse(storage.getItem("musicList"));
	$("#localList").empty();
				for(var i=0;i< musicList.length;i++) {
						var music=musicList[i];
						var li=' <li>'+
	                       '     <a >'+music.name+'</a>'+
						   '     <a href="javascript:playPosition='+i+';playMusic(\''+i+'\');" data-role="button"></a>'+
	                       ' </li>';
					   $("#localList").append(li);
				}
				
				$("#localList").listview('refresh');
}

